//
//  ComiContentViewController.m
//  Comic
//
//  Created by 程锦 on 16/3/17.
//  Copyright © 2016年 程锦. All rights reserved.
//

#import "ComiContentViewController.h"
#import "ComicContentTableViewCell.h"
#import "ComicContentModel.h"
#import "AuthorViewController.h"

#define Data @"http://api.kuaikanmanhua.com/v1/comics/"
@interface ComiContentViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UIView *firstView;
@property (weak, nonatomic) IBOutlet UIImageView *namePhoto;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UITableView *comicTableView;
@property (weak, nonatomic) IBOutlet UIButton *skip;
@property (nonatomic ,strong)NSMutableArray *comic;
@property (nonatomic ,copy) NSString *url;

@end

@implementation ComiContentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    // 数据解析
    [self createData];
    // 创建TableView
    [self createView];
}

#pragma mark 数据解析
- (void)createData{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *str = [NSString stringWithFormat:@"%@%@",Data,self.idNUmber];
    [manager GET:str parameters:self success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject[@"data"];
        self.comic = [NSMutableArray array];
        self.comic = dic[@"images"];
        NSString *photo = [[dic[@"topic"] objectForKey:@"user"]objectForKey:@"avatar_url"];
        [self.namePhoto sd_setImageWithURL:[NSURL URLWithString:photo]];
        self.nameLabel.text = [[dic[@"topic"]objectForKey:@"user"]objectForKey:@"nickname"];
        self.url = [[dic[@"topic"]objectForKey:@"user"]objectForKey:@"id"];
        [self.comicTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
    
}

#pragma mark 创建TableView
- (void)createView{
    self.title = self.nameTitle;
    self.namePhoto.layer.cornerRadius = 21;
    self.namePhoto.layer.masksToBounds = YES;
    self.comicTableView.tableHeaderView = self.firstView;
    UINib *cellNib = [UINib nibWithNibName:NSStringFromClass([ComicContentTableViewCell class]) bundle:nil];
    [self.comicTableView registerNib:cellNib forCellReuseIdentifier:NSStringFromClass([ComicContentTableViewCell class])];
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    rightButton.frame = CGRectMake(0, 0, 18, 18);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    [rightButton addTarget:self action:@selector(clickleft:) forControlEvents:UIControlEventTouchUpInside];
    [self.skip addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    // 隐藏分割线
    self.comicTableView.separatorStyle = UITableViewCellSelectionStyleNone;
}

#pragma mark tableview协议

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.comic.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ComicContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ComicContentTableViewCell class]) forIndexPath:indexPath];
    [cell.photo setImageWithURL:[NSURL URLWithString:self.comic[indexPath.row]] placeholderImage:[UIImage imageNamed:@"fakeimg"]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return SCREEN_HEIGHT / 3 + 20;
}

- (void)click:(UIButton *)button{
    
    AuthorViewController *authorVC = [[AuthorViewController alloc] init];
    authorVC.authorUrl = self.url;
    [self.navigationController pushViewController:authorVC animated:YES];
}

- (void)clickleft:(UIButton *)button{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 取消选中状态
    [self.comicTableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
